package edu.xpu.project.vehicle.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.xpu.project.vehicle.entity.BusSchedule;
import edu.xpu.project.vehicle.service.BusScheduleService;


/**
 * @describe 校车信息时刻的控制器
 * @author liukang
 * @date 2017年5月11日
 */
@Controller
@RequestMapping(value="busSchedule")
public class BusScheduleController {
	
	@Autowired
	private BusScheduleService busScheduleService;
	
	/**
	  * @MethodName: busScheduleDetails
	  * @Description: 校车时刻详情
	  * @Author liukang
	  * @date 2017年5月23日  下午12:19:42
	 */
	@RequestMapping(value="busScheduleDetails/{id}")
	public String busScheduleDetails(@PathVariable(value="id") Long id, ModelMap modelMap) {
		BusSchedule busSchedule = busScheduleService.get(id);
		modelMap.addAttribute("busSchedule", busSchedule);
		return "busSchedule/busScheduleDetail";
	}
	
	/**
	 * @describe 校车时刻删除
	 * @date 2017年5月23日
	 * @author liukang
	 */
	@ResponseBody
	@RequestMapping(value="busScheduleDel/{id}")
	public String busScheduleDel(@PathVariable(value="id") Long id) {
		try {
			busScheduleService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return "{\"status\":\"error\"}";
		}
		return "{\"status\":\"success\"}";
	}
	
	
	/**
	 * @describe 校车保存页面
	 * @date 2017年5月11日
	 * @author liukang
	 */
	@RequestMapping(value="busScheduleSave")
	public String busScheduleSave(BusSchedule busSchedule) {
		BusSchedule saveObj = null;
		
		if (busSchedule.getId() == null) {
			busSchedule.setCreateDate(new Date());
			saveObj = busSchedule;
		} else {
			BusSchedule busSchedule2 = busScheduleService.get(busSchedule.getId());
			busSchedule2.setStartTime(busSchedule.getStartTime());
			busSchedule2.setEndTime(busSchedule.getEndTime());
			busSchedule2.setStartStation(busSchedule.getStartStation());
			busSchedule2.setEndStation(busSchedule.getEndStation());
			saveObj = busSchedule2;
		}
		busScheduleService.saveOrUpdate(saveObj);
		return "redirect:busScheduleList";
	}
	
	/**
	 * @describe 定向到校车时刻添加页面
	 * @date 2017年5月11日
	 * @author liukang
	 */
	@RequestMapping(value="toBusScheduleAdd")
	public String toBusScheduleAdd() {
		
		return "busSchedule/busScheduleAdd";
	}
	
	/**
	 * @describe 列车时刻表展示
	 * @date 2017年5月12日
	 * @author liukang
	 */
	@RequestMapping(value="busSceduleIndex")
	public String busScheduleListForTeacher(@RequestParam(value="pageNumber", defaultValue="1", required=false) String pageNumber,
			@RequestParam(value="pageSize", defaultValue="10", required=false) String pageSize,
			BusSchedule busSchedule, ModelMap modelMap) {
		Page<BusSchedule> pageByCriterias = busScheduleService.getPageByCriterias(pageNumber, pageSize, busSchedule);
		//可以做适当的业务操作
		modelMap.addAttribute("page", pageByCriterias)
				.addAttribute("number", pageNumber)
				.addAttribute("size", pageSize)
				.addAttribute("searchVal", busSchedule);
		return "teacher/index/busSceduleIndex";
	}
	
	
	/**
	 * @describe 校车时刻表
	 * @date 2017年5月11日
	 * @author liukang
	 */
	@RequestMapping(value="busScheduleList")
	public String busScheduleList(@RequestParam(value="pageNumber", defaultValue="1", required=false) String pageNumber,
			@RequestParam(value="pageSize", defaultValue="10", required=false) String pageSize,
			BusSchedule busSchedule, ModelMap modelMap) {
		Page<BusSchedule> pageByCriterias = busScheduleService.getPageByCriterias(pageNumber, pageSize, busSchedule);
		modelMap.addAttribute("page", pageByCriterias)
				.addAttribute("number", pageNumber)
				.addAttribute("size", pageSize)
				.addAttribute("searchVal", busSchedule);
		return "busSchedule/busScheduleList";
	}

}
